package com.xmb.quick.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.xmb.quick.entity.SysDept;
import com.xmb.quick.entity.SysRole;
import com.xmb.quick.entity.SysUser;
import com.xmb.quick.mapper.SysUserMapper;
import com.xmb.quick.service.ISysDeptService;
import com.xmb.quick.service.ISysRoleService;
import com.xmb.quick.service.ISysUserService;
import com.xmb.quick.util.ConditionUtil;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangping
 * @since 2020-03-11
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

	@Autowired
	private ISysDeptService iSysDeptService;
	
	@Autowired
	private ISysRoleService iSysRoleService;
	
	@Override
	public List<SysUser> selectInfoList(Wrapper<SysUser> ew) {
		return selectList(ew);
	}

	@Override
	public SysUser selectInfoOne(Wrapper<SysUser> ew) {
		return selectOne(ew);
	}

	@Override
	public Page<SysUser> selectInfoPage(Page<SysUser> page, Wrapper<SysUser> ew, String dept, String role) throws Exception{
		if(StringUtils.isNotEmpty(dept)) {
			List<SysDept> deptList = iSysDeptService.selectList(ConditionUtil.convert(dept, new EntityWrapper<SysDept>()));
			Set<String> deptIds = new HashSet<String>();
			deptList.stream().forEach(e -> {
				deptIds.add(e.getId());
			});
			ew.in("department_id", deptIds);
		}
		if(StringUtils.isNotEmpty(role)) {
			List<SysRole> roleList = iSysRoleService.selectList(ConditionUtil.convert(dept, new EntityWrapper<SysRole>()));
			Set<String> roleIds = new HashSet<String>();
			roleList.stream().forEach(e -> {
				roleIds.add(e.getId());
			});
			ew.in("role_id", roleIds);
		}
		return selectPage(page,ew);
	}

	@Override
	public void add(SysUser user) throws Exception {
		insert(user);
	}

	@Override
	public void edit(SysUser user) throws Exception {
		updateById(user);
	}

	@Override
	public void deleteInfo(List<String> ids) throws Exception {
		deleteBatchIds(ids);
	}

}
